package UserInput;

import idp.MainController;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.*;
import debuggen.Reader;
import Command.*;
import Dashboard.*;

/**
 * This class handles all the events originating from the dashboard
 * 
 * @author Sietse
 *
 */
public class DashboardInput implements UserInput, 
									   MouseListener, 
									   ActionListener,
									   ItemListener
{
	private MainController mController;
	
	//Simple sound clip for the stop button
	private AudioClip clip; 
	
	//The default selected replay
	private String selectedReplay = "1"; //Initialize it on the default value!!
	private String selectedFrequency = "10x";

	
	/**
	 * 
	 * @param p_mcController the MainController
	 */
	public DashboardInput(MainController p_mcController)
	{
		mController = p_mcController;
		clip = Applet.newAudioClip(this.getClass().getResource("/Resources/ding.wav"));
	}
	
	@Override
	public void itemStateChanged(ItemEvent event) //For the Choice widgets
	{
		MenuNames choiceName = MenuNames.valueOf(((Choice)event.getSource()).getName());
		
		switch(choiceName)
		{
			case CHOOSEREPLAY: 
				selectedReplay = ((Choice)event.getSource()).getSelectedItem();
				break;
			case CHOOSEFREQUENCY:
				selectedFrequency = ((Choice)event.getSource()).getSelectedItem();
				break;
		}
	}
	
	//@Override
	public void actionPerformed(ActionEvent event) 
	{		
		//If it's a button...
		if(event.getSource() instanceof Button)
		{
			MenuNames menuName = MenuNames.valueOf(((Button)event.getSource()).getName());
			
			switch(menuName)
			{
				case NEWSESSION: mController.createDatabaseSession();
					break;
				case STARTREPLAY:
					mController.requestReplay(Integer.valueOf(selectedReplay), Integer.valueOf(selectedFrequency.substring(0, selectedFrequency.length() - 1)));
					break;
				case CONNECT: 
					mController.startBluetooth();
					((Button)event.getSource()).setLabel("Disconnect");
					((Button)event.getSource()).setName(String.valueOf(MenuNames.DISCONNECT));
					break;	
				case DISCONNECT:
					mController.stopBluetooth();
					((Button)event.getSource()).setLabel("Connect");
				  	((Button)event.getSource()).setName(String.valueOf(MenuNames.CONNECT));
					break;
				case RESETDATABASE:
					mController.resetDatabase();
					break;
			}
		}
		else if(event.getSource() instanceof JComboBox)
		{
			selectedReplay = String.valueOf(((JComboBox)event.getSource()).getSelectedItem());
		}
	}
	@Override
	public void mouseClicked(MouseEvent event) 
	{
		//Get the name of the button
		ButtonNames buttonName = ButtonNames.valueOf(((JLabel)event.getSource()).getName());	


		//Do something depending on which button was clicked on
		switch(buttonName)
		{
			case CONTROLAUTOMATIC: 
				mController.processUserInput(new ControlAutomatic());
				break;
			case CONTROLMANUAL: 
				mController.processUserInput(new ControlManual());
				break;
			case KEELUP: 
				mController.processUserInput(new KeelUp());
				break;
			case KEELDOWN: 
				mController.processUserInput(new KeelDown());
				break;
			case MOTORFORWARD: 
				mController.processUserInput(new EngineForward());
				break;
			case MOTORBACKWARD: 
				mController.processUserInput(new EngineBackward());
				break;
			case MOTORNEUTRAL:
				mController.processUserInput(new EngineOut());
				break;
			case EMERGENCYSITUATIONON:
				mController.processUserInput(new UndoEmergencySituation());
				((JLabel)event.getSource()).setName(ButtonNames.EMERGENCYSITUATIONOFF.name());
				break;
			case EMERGENCYSITUATIONOFF: 
				mController.processUserInput(new EmergencySituation());
				((JLabel)event.getSource()).setName(ButtonNames.EMERGENCYSITUATIONON.name());
				break;
			case EMERGENCYSTOP: 
				mController.processUserInput(new EmergencyStop());
				clip.play();
				break;
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) 
	{
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseExited(MouseEvent arg0) 
	{
		// TODO Auto-generated method stub	
	}

	@Override
	public void mousePressed(MouseEvent arg0) 
	{
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) 
	{
		// TODO Auto-generated method stub		
	}


}
